//
//  API_Multithreading_SafeArray_Demo3VC.swift
//  APSwift
//
//  Created by jianbo.pei on 2023/7/25.
//

import UIKit

class API_Multithreading_SafeArray_Demo3VC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        test1()

    }
}


extension API_Multithreading_SafeArray_Demo3VC {
    private func test1() {
        // 创建 MyActor 实例
        let myActor = MyActor()
         
        /// 异步读取共享数据
        Task {
            for data in await myActor.readData() {
                print("read data: \(data)")
            }
        }
         
        // 异步写入共享数据
        Task {
            await myActor.writeData(value: 10)
            print("write Data 10")
        }
         
        // 异步读取共享数据
        Task {
            for data in await myActor.readData() {
                print("read data: \(data)")
            }
        }
         
        // 结果
        // read data: 1
        // read data: 2
        // read data: 3
        // read data: 4
        // read data: 5
        // write Data 10
        // read data: 1
        // read data: 2
        // read data: 3
        // read data: 4
        // read data: 5
        // read data: 10
        // 异步读写数据，数据安全
    }
}
